<?php
/**
 * 项目名称-湘上健康
 * 统计定时
 *
 * User: kaikaic
 * Date: 2018/5/28
 * Time: 13:38
 */

namespace app\command\controller;

use service\StatisticsService;
use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\Db;

class Statistic extends Command {

    /**
     * 任务名称, php think $taskName
     */
    private $taskName = "Statistic";

    protected function configure(){
        $this->setName($this->taskName)->setDescription("计划任务-数据统计-订单数据");
    }

    protected function execute(Input $input, Output $output){
        $output->writeln('Date Crontab job start...');
        $output->writeln("");

        $this->runTotalTask();

        $this->runOrderTask();

        $output->writeln("");
        $output->writeln('Date Crontab job end...');
    }
    public function executeSum(){
//        $output->writeln('Date Crontab job start...');
//        $output->writeln("");

        $this->runTotalTask();

        $this->runOrderTask();

//        $output->writeln("");
//        $output->writeln('Date Crontab job end...');
    }
    /**
     * 每日订单情况统计
     */
    private function runOrderTask() {
        $startTime = strtotime(date("Y-m-d", strtotime("-1 day")));
        $endTime = strtotime(date("Y-m-d", time()));

        $db = Db::name("ExamOrder");

        // 已支付+与预约
//        $where = " create_time BETWEEN {$startTime} and {$endTime} AND order_status=2 AND pay_status=2";
        $where = " create_time BETWEEN {$startTime} and {$endTime}";

        // 订单数
        $record = $db->query("SELECT count(*) AS count FROM exam_order WHERE " . $where);
        $totalOrder = $record[0]['count'];

        // 用户数
        $record = $db->query("SELECT count(DISTINCT account_id) AS count FROM exam_order WHERE " . $where);
        $totalUser = $record[0]['count'];

        // 体检中心数
        $record = $db->query("SELECT count(DISTINCT mec_id) AS count FROM exam_order WHERE " . $where);
        $totalMec = $record[0]['count'];

        // 体检套餐数
        $record = $db->query("SELECT count(DISTINCT set_id) AS count FROM exam_order WHERE " . $where);
        $totalSet = $record[0]['count'];

        // 订单金额
        $record = $db->query("SELECT SUM(price) AS sum FROM exam_order WHERE " . $where);
        $totalPrice = round($record[0]['sum'], 0);


        $params = array();
        $params['num_order'] = $totalOrder;
        $params['num_user'] = $totalUser;
        $params['num_mec'] = $totalMec;
        $params['num_set'] = $totalSet;
        $params['total_price'] = $totalPrice;
        $params['create_time'] = $startTime;

        $record = StatisticsService::getInstance()->find($startTime, 'create_time', 1);
        if (!$record) {
            StatisticsService::getInstance()->insert($params, 1);

            echo "run order task finished.\r\n";
        }else {
            echo "record is exist already!\r\n\r\n";
        }
    }

    /**
     * 总数据量统计
     */
    private function runTotalTask() {
        $startTime = strtotime(date("Y-m-d", strtotime("-1 day")));
        $endTime = strtotime(date("Y-m-d", time()));

        $db = Db::name("Account");

        // 总用户数
        $record = $db->query("SELECT count(*) AS count FROM account");
        $totalMember = $record[0]['count'];

        // 总企业数
        $record = $db->query("SELECT count(*) AS count FROM exam_enterprise");
        $totalEnterprise = $record[0]['count'];

        // 总体检中心数
        $record = $db->query("SELECT count(*) AS count FROM exam_mec");
        $totalMec = $record[0]['count'];

        // 总体检套餐数
        $record = $db->query("SELECT count(*) AS count FROM exam_set");
        $totalSet = $record[0]['count'];

        // 总订单数
        $record = $db->query("SELECT count(*) AS count FROM exam_order");
        $totalOrder = $record[0]['count'];

        $params = array();
        $params['num_user'] = $totalMember;
        $params['num_enterprise'] = $totalEnterprise;
        $params['num_mec'] = $totalMec;
        $params['num_set'] = $totalSet;
        $params['num_order'] = $totalOrder;
        $params['create_time'] = $startTime;

        $record = StatisticsService::getInstance()->find($startTime, 'create_time');
        if (!$record) {
            StatisticsService::getInstance()->insert($params);

            echo "run total task finished.\r\n";
        }else {
            echo "record is exist already!\r\n\r\n";
        }
    }
}